package rabc.veiw;

import rabc.domain.dto.DeptDto;
import rabc.domain.dto.UserDto;
import rabc.domain.vo.DeptVo;
import rabc.domain.vo.Result;
import rabc.domain.vo.RoleVo;
import rabc.domain.vo.UserVo;
import rabc.service.Deptservice;
import rabc.service.UserService;

public class  UserVeiw {
    public static UserVo[] cache;

    public static  void show() {
        String menuChoice;
        do {
            printMenu();
            menuChoice = MainVeiw.input.nextLine();
            switch (menuChoice) {
                case "0":
                    MainVeiw.show();
                    break;
                case "1":
                    list();
                    break;
                case "2":
                    create();
                    break;
                case "3":
                    update();
                    break;
                case "4":
                    toggle();
                    break;
                default:
                    System.out.println();
            }
        }while(!"0".equals(menuChoice));
        MainVeiw.input.close();
    }
    public static void  printMenu(){

        String Menu= """
                -------------------
                    部门管理
                -------------------
                | 0 返回上级        |
                | 1 用户列表        |
                | 2 创建用户        |
                | 3 更新用户        |
                | 4 切换状态        |
                -------------------
                请输入选项：
                """;
        System.out.println(Menu);
    }
    public static void printError(String message){
        System.out.println("\033[31m"+message+"\033[0m"  );
    }
    public static void list(){
        Result result = UserService.list();
        cache=(UserVo[]) result.data;
        System.out.println("ID\t用户名称\t姓名\t手机\t邮箱\t部门名称\t角色名称\t状态");
        for (UserVo user : cache) {
            System.out.printf("%d\t%s\t%s\t%s\t%s\t%s\t%s\t%s\n",user.id,user.username,
                    user.name, user.mobile,user.email,user.deptName,user.roleName,user.enable?"启用":"禁用");
        }
    }
    public static void create() {
        System.out.println("请输入用户名称：");
        String username = MainVeiw.input.nextLine();
        System.out.println("请输入姓名：");
        String name = MainVeiw.input.nextLine();
        System.out.println("请输入手机号码：");
        String mobile = MainVeiw.input.nextLine();
        System.out.println("请输入邮箱：");
        String email = MainVeiw.input.nextLine();
        System.out.println("请输入部门ID：");
        int deptId = 0;
        do{
            DeptVeiw.list();
            if(DeptVeiw.cache==null||DeptVeiw.cache.length==0){
                printError("没有部门信息，请先创建部门信息");
                return;
            }
            DeptVo dept =DeptVeiw.getCacheById();
            if(dept!=null){
                deptId = dept.id;
            }
        }while (deptId==0);
        System.out.print("请输入角色ID：");
        int roleId = 0;
        do{
            RoleVeiw.list();
            if(RoleVeiw.cache==null||RoleVeiw.cache.length==0){
                printError("没有角色信息，请先创建角色信息");
                return;
            }
            RoleVo role =RoleVeiw.getCacheBy();
            if(role!=null){
                roleId = role.id;
            }
        }while (roleId==0);
        UserDto dto = new UserDto(deptId,email,mobile,name,roleId,username);
        Result result = UserService.create(dto);
        if(!result.success){
            printError(result.message);
            return;
        }
        System.out.println(result.message);
        list();
    }
    public static void update(){
        UserVo user = getCacheById();
        if (user==null){
            return;
        }
        System.out.print("请输入用户名称：直接回车表示不修改");
        String username = MainVeiw.input.nextLine();
        username="".equals(username)?user.username:username;

        System.out.print("请输入姓名：直接回车表示不修改");
        String name = MainVeiw.input.nextLine();
        name="".equals(name)?user.name:name;
        System.out.print("请输入电话：直接回车表示不修改");
        String mobile = MainVeiw.input.nextLine();
        mobile="".equals(mobile)?user.mobile:mobile;
        System.out.print("请输入邮箱：直接回车表示不修改");
        String email = MainVeiw.input.nextLine();
        email="".equals(email)?user.email:email;
        System.out.println("请输入部门ID：");
        int deptId = 0;
        do{
            DeptVeiw.list();
            if(DeptVeiw.cache==null||DeptVeiw.cache.length==0){
                printError("没有部门信息，请先创建部门信息");
                return;
            }
            DeptVo dept =DeptVeiw.getCacheById();
            if(dept!=null){
                deptId = dept.id;
            }
        }while (deptId==0);
        System.out.print("请输入角色ID：");
        int roleId = 0;
        do{
            RoleVeiw.list();
            if(RoleVeiw.cache==null||RoleVeiw.cache.length==0){
                printError("没有角色信息，请先创建角色信息");
                return;
            }
            RoleVo role =RoleVeiw.getCacheBy();
            if(role!=null){
                roleId = role.id;
            }
        }while (roleId==0);


        UserDto dto = new UserDto(deptId,email,mobile,name,roleId,username);;
        Result result = UserService.update(dto,user.id);
        if(!result.success){
            printError(result.message);
            return;
        }
        System.out.println(result.message);
        list();
    }
    public static void toggle(){
        UserVo user = getCacheById();
        if (user==null){
            return;
        }
        if(user.enable){
            System.out.println("确认禁用吗？y确认，其他取消");
        }else {
            System.out.println("确认启用吗？y确认，其他取消");
        }
        String confirm = MainVeiw.input.nextLine();
        if ("y".equalsIgnoreCase(confirm)) {
            System.out.println("您已取消修改状态操作");
        }
        Result result = UserService.toggle(user.id);
        if(!result.success){
            printError(result.message);
            return;
        }
        System.out.println(result.message);
        list();
    }
    public static UserVo getCacheById(){
        System.out.println("请输入id编号：id必须是大于零的整数");
        if (!MainVeiw.input.hasNextInt()){
            MainVeiw.input.nextLine();
            System.out.println("id必须是整数，请输入整数");
            return null;
        }
        int id = MainVeiw.input.nextInt();
        MainVeiw.input.nextLine();
        if (id <= 0){
            printError("id必须是大于零的整数");
            return null;
        }
        if(cache==null){
            Result result = UserService.list();
            cache=(UserVo[]) result.data;
        }
        UserVo user =null;
        for (UserVo item : cache) {
            if (item.id == id) {
                System.out.println("该ID对应的数据是：");
                System.out.printf("ID%d\t用户名%s\t姓名%s\t手机%s\t邮箱%s\t部门名称%s\t角色名称%s\t状态%s\n",user.id,user.username,
                        user.name, user.mobile,user.email,user.deptName,user.roleName,user.enable?"启用":"禁用");
                user=item;
                break;
            }
        }
        if (user==null){
            System.out.println("没有找到对应的数据");
            return null;
        }
        return user;
    }

}
